package com.jk.controller;

import com.jk.pojo.PayShopsCoupon;
import com.jk.pojo.PayUser;
import com.jk.service.PayUserCouponService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by scc on 2017/12/18.
 */
@RestController
@RequestMapping("app/payUserCoupon")
public class PayUserCouponController {

    /**
     * 按byType自动注入用户优惠券service层
     */
    @Autowired
    private PayUserCouponService payUserCouponService;

    /**
     * 查询此用户共有多少张优惠券
     * @return couponCount
     */
    @GetMapping("queryPayUserCouponCount")
    public int queryPayUserCouponCount(HttpServletRequest request){
        //获取session中的用户信息,拿到用户id用来查询此用户共有多少张优惠券
        PayUser user = (PayUser)request.getSession().getServletContext().getAttribute("us");
        //通过用户id查询出此用户共有多少张优惠券
        int couponCount = payUserCouponService.queryPayUserCouponCount(user.getUserid());
        //rabbitTemplate.convertAndSend("userid",userid);
        //将查询到的优惠券总条数返回给前台进行展示
        return couponCount;
    }

    /**
     *根据用户id查询出用户所拥有的具体的优惠券信息
     * request请求作用域, page当前页, rows每页条数
     * @param request page rows
     * @return list
     */
    @GetMapping("queryPayShopsCouponList")
    public Map<String,Object> queryPayShopsCouponList(HttpServletRequest request){
        //获取session中的用户信息,拿到用户id用来查询此用户所拥有的具体的优惠券信息
        PayUser user = (PayUser)request.getSession().getServletContext().getAttribute("us");
        int page = Integer.parseInt(request.getParameter("page"));
        int rows = Integer.parseInt(request.getParameter("rows"));
        //通过用户id来查询出该用户所拥有的所有可用优惠券信息  分页查询
        Map<String,Object> map = payUserCouponService.queryPayShopsCouponList(user.getUserid(),page,rows);
        //将查询到的分页数据返回到前台展示
        return map;
    }

    /**
     * 用户根据优惠券id领取优惠券
     * @param request
     * @return
     */
    @PostMapping("insertPayUserCoupon")
    public Map<String,Object> insertPayUserCoupon(HttpServletRequest request,String couponid){
        PayUser user = (PayUser)request.getSession().getServletContext().getAttribute("us");
        //到service层进行业务逻辑处理
        Map<String,Object> map = payUserCouponService.insertPayUserCoupon(couponid,user.getUserid());
        //将返回的信息返回到前台
        return map;
    }

    /**
     * 查询商家优惠券,用于用户领取
     * @param
     * @param
     * @return
     */
    @GetMapping("queryShopsCouponListAll")
    public List<Map<String,Object>> queryShopsCouponListAll(){
        //到service层进行业务逻辑处理
        List<Map<String,Object>> list = payUserCouponService.queryShopsCouponListAll();
        //将返回的信息返回到前台
        return list;
    }

    /**
     * 根据用户优惠券进行支付抵扣
     // @param shopid 商家id
     // @param ucid 用户优惠券id
     * @param goodsPrice 商品价格
    // @param couponid 优惠券id
     * @return
     */
    @PutMapping("paymentByPayUserCoupon")
    public Map<String,Object> paymentByPayUserCoupon(HttpServletRequest request,Double goodsPrice,String coid,String paymentPwd){
        List<Map<String,Object>> list = payUserCouponService.queryPayShopsUserCoupon(coid);
        String ucid = "";
        String shopid = "";
        for (int i = 0;i<list.size();i++){
            ucid = (String)list.get(i).get("ucId");
            shopid = (String)list.get(i).get("shopId");
        }

        PayUser user = (PayUser)request.getSession().getServletContext().getAttribute("us");
        //将参数传到业务处理层进行业务处理
        Map<String,Object> map = payUserCouponService.paymentByPayUserCoupon(user.getUserid(),ucid,shopid,goodsPrice,coid,paymentPwd);
        //返回对应信息
        return map;

    }

    /**
     * 添加商家优惠券
     * @param PayShopsCoupon  商家优惠券对象
     * @param shopId 商家id
     * @return
     */
    @PostMapping("insertPayShopsCoupon")
    public Map<String,Object> insertPayShopsCoupon(PayShopsCoupon PayShopsCoupon,String shopId){
        //将对象传入业务处理层进行添加
        Map<String,Object> map = payUserCouponService.insertPayShopsCoupon(PayShopsCoupon,shopId);
        //返回对应信息
        return map;
    }

    @PutMapping("paymentByPayUser")
    public Map<String,Object> paymentByPayUser(HttpServletRequest request,Double goodsPrice,String paymentPwd){

        PayUser user = (PayUser)request.getSession().getServletContext().getAttribute("us");
        //将参数传到业务处理层进行业务处理
        Map<String,Object> map = payUserCouponService.paymentByPayUser(user.getUserid(),goodsPrice,paymentPwd);
        //返回对应信息
        return map;

    }

}
